using System;
using System.Reflection;
using System.Xml;
using XmlObjects;
using XmlObjects.XmlObjectHydrator;
using XmlObjects.XmlObjectHydrator.Parsers;
using MbUnit.Framework;

namespace UnitTests.Hydrator
{
	[TestFixture]
	public class TestHydrator
	{
		[Test]
		public void TestFoo()
		{
			XmlDocument document = new XmlDocument();
			XmlNode node = document.CreateElement("Foobody");
			document.AppendChild(node);
			XmlAttribute attribute = document.CreateAttribute("NooBody");
			attribute.InnerXml = "DooBody";
			node.AppendChild(attribute);
		}
		[Test]
		public void TestNine()
		{
			XmlAttribute attribute = myDoc.CreateAttribute("Attribute");
			attribute.Value = "3";
			Parseable <int> parseable = new Parseable<int>();
			Assert.AreEqual(3, parseable.GetValue(attribute));
			Type theType = typeof (Int32);
			MethodInfo info = theType.GetMethod("Parse", new Type[] {typeof(string)});
			int foo = (int) info.Invoke(2, new object[] {"3"});
			foo.ToString();
		}
		[Test]
		public void TestOne()
		{
			MemberInfo memberInfo = myObject.GetType().GetProperty("AttributeTwo");
			Assert.IsNotNull(memberInfo);

			Attribute.IsDefined(memberInfo, typeof (PropertyAttribute));
			Attribute attribute = Attribute.GetCustomAttribute(memberInfo, typeof (PropertyAttribute));
			attribute.ToString();
		}

		private XmlDocument myDoc;
		protected MyObject myObject;
		protected string attributeOne = "attributeOne";
		protected int attributeTwo = 2;
		protected string elementOne = "elementOne";
		protected int elementTwo = 2;
		protected double elementThree = 3.1415926;
		protected string myProperty = "SomethingCool";

		[SetUp]
		public void SetUp()
		{
			myDoc = new XmlDocument();
			myDoc.Load("SomeDocument.xml");
			myObject = new MyObject();
			XmlNode parentNode = myDoc.SelectSingleNode("/document/myObject");
		}

		protected MyObject GetAMyObject()
		{
			MyObject aMyObject = new MyObject();
			aMyObject.AttributeOne = attributeOne;
			aMyObject.AttributeTwo = attributeTwo;
			aMyObject.ElementOne = elementOne;
			aMyObject.ElementTwo = elementTwo;
			aMyObject.ElementThree = elementTwo;
			MyCoolAnotherObject o = new MyCoolAnotherObject();
			o.MyProperty = myProperty;
			aMyObject.MyAnotherObject = o;
			return aMyObject;

		}


		[Serializeable("myObject")]
		public class MyObject
		{
			private string attributeOne;
			private int attributeTwo;
			private string elementOne;
			private int elementTwo;
			private double elementThree;
			private MyObject myObject;
			private MyAnotherObject myAnotherObject;

			[Attrib("attributeOne")]
			public string AttributeOne
			{
				get { return attributeOne; }
				set { attributeOne = value; }
			}

			[Attrib("attributeTwo")]
			public int AttributeTwo
			{
				get { return attributeTwo; }
				set { attributeTwo = value; }
			}

			[Property("elementOne")]
			public string ElementOne
			{
				get { return elementOne; }
				set { elementOne = value; }
			}

			[Property("elementTwo")]
			public int ElementTwo
			{
				get { return elementTwo; }
				set { elementTwo = value; }
			}

			[Property("elementThree")]
			public double ElementThree
			{
				get { return elementThree; }
				set { elementThree = value; }
			}

			[Property("objectElementOne")]
			public MyObject ObjectElementOne
			{
				get { return myObject; }
				set { myObject = value; }
			}

			[Property("myAnotherObject")]
			public MyAnotherObject MyAnotherObject
			{
				get { return myAnotherObject; }
				set { myAnotherObject = value; }
			}

			private MyAnotherObject myCoolObject;

			[Property("myCoolObject", FindSubTypeBy = "type")]
			[SubType("foobar", typeof(MyAnotherObject), IsDefault = true)]
			public MyAnotherObject MyCoolObject
			{
				get { return myCoolObject; }
				set { myCoolObject = value; }
			}
		}

		public class MyAnotherObject
		{
		}

		public class MyCoolAnotherObject : MyAnotherObject
		{
			private string myProperty;

			[Property("myProperty")]
			public string MyProperty
			{
				get { return myProperty; }
				set { myProperty = value; }
			}
		}

	}
}